<template>
  <el-row>
    <el-col>
      <el-table
          :data="tableData"
          height="700"

      >
        <el-table-column
            label="用户名"
            prop="name"
        >
        </el-table-column>
        <el-table-column
            label="答题数量"
            prop="number"
        >
        </el-table-column>
        <el-table-column
            label="最后答题时间"
            prop="zeroDate"
            sortable
        >
        </el-table-column>
        <el-table-column
            label="总得分"
            prop="fraction"
            sortable
        >
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
</template>

<script>
export default {
  name: "Class",
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      input: "",
      number: "",
      zeroDate: "",
      userName: "",
    }
  },
  mounted() {
    function formatDate(date) {
      let d = new Date(date),
          month = '' + (d.getMonth() + 1),
          day = '' + d.getDate(),
          year = d.getFullYear(),
          hour = d.getHours(),
          minute = d.getMinutes();

      if (month.length < 2) month = '0' + month;
      if (day.length < 2) day = '0' + day;
      let yearTime = [year, month, day].join('-')
      let dayTime = [hour, minute].join(':')
      return yearTime + " " + dayTime;
    }

    this.$axios2.post("/grades/info").then(async (res) => {
      let data = res.data.data
      // 获取后端用户和得分情况
      for (let i = 0; i < data.length; i++) {
        let list = {}
        list.name = data[i].username
        list.fraction = data[i].Fraction
        await this.$axios2.post("/grades/date", {
          "userName": data[i].username
        }).then((res) => {
          let data = res.data.data
          if (data.length > 0) {
            this.number = data.length
            this.zeroDate = formatDate(data.pop().UpdatedAt)
            list.number = this.number
            list.zeroDate = this.zeroDate
          } else {
            this.number = 0
            this.zeroDate = "未答题！"
            list.number = this.number
            list.zeroDate = this.zeroDate
          }
        })
        this.tableData.push(list)
      }
    })
  },
  methods: {}
}
</script>

<style scoped>

</style>